#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structure_donnee.h"



/** Constructeur : retourne un pointeur vers une structure initialisee
*/
Parametres * CreerParametre( )
{
	Parametres  *P;
	P = (Parametres*) malloc ( sizeof ( Parametres));

	if ( InitialiseParametres( P) )
        return P;
    else
        return NULL;
}

/** Destructeur : desaloue la structure
Type de retour standard
*/
int DestructeurParametre ( Parametres * P )
{

    free(P);

    return 1;
}



/** set */
int InitialiseParametres 	(Parametres * P)
{
  strcpy( P->serveur, "localhost");
  strcpy(P->port, "21");
  strcpy(P->FIC, "Pas de fichier");
  strcpy( P->user, "anonymous");
  strcpy( P->password, "default");
  P->debug = 0;

  return 1;
}

int AffecteServeur	(const char * parametre, Parametres * P)
{
	int ret= 1;
	if ( P == NULL )
	   ret = -2;
	else if ( parametre == NULL )
	   ret = -3;
	else
	{

		strcpy( P->serveur, parametre );
		if ( strcmp( parametre, P->serveur) != 0)
			ret = -1;
	}
	return ret;
}

int AffectePort		(const char * parametre, Parametres * P)
{
	int ret= 1;
	if ( P == NULL )
	   ret = -2;
	else if ( parametre == NULL )
	   ret = -3;
	else
	{
		strcpy( P->port, parametre );
		if ( strcmp( parametre, P->port) != 0)
			ret = -1;
	}
	return ret;
}

int AffecteDebug	(const int    parametre, Parametres * P)
{
	int ret= 1;
	if ( P == NULL )
	   ret = -2;
	else if ( parametre == 0 )
	   ret = -3;
	else
	{
		P->debug = parametre;
		if ( P->debug != parametre)
			ret = -1;
	}
	return ret;
}

int AffecteUser 	(const char * parametre, Parametres * P)
{
	int ret= 1;
	if ( P == NULL )
	   ret = -2;
	else if ( parametre == NULL )
	   ret = -3;
	else
	{
	    strcpy( P->user, parametre );
		if ( strcmp( parametre, P->user) != 0)
			ret = -1;
	}
	return ret;
}

int AffectePassword	(const char * parametre, Parametres * P)
{
	int ret= 1;
	if ( P == NULL )
	   ret = -2;
	else if ( parametre == NULL )
	   ret = -3;
	else
	{
		strcpy( P->password, parametre );
		if ( strcmp( parametre, P->password) != 0)
			ret = -1;
	}
	return ret;
}
int AffecteFic	 	(const char * parametre, Parametres * P)
{
	int ret= 1;
	if ( P == NULL )
	   ret = -2;
	else if ( parametre == NULL )
	   ret = -3;
	else
	{
		strcpy( P->FIC, parametre );
		if ( strcmp( parametre, P->FIC) != 0)
			ret = -1;
	}
	return ret;
}


/** Get */
int RecupereServeur	(char * retour, const Parametres * P)
{
	int ret=1;
	if (P == NULL)
		ret = -2;
	else
	{
		strcpy( retour, P->serveur );
		if ( strcmp( retour, P->serveur) != 0)
			ret = -1;
	}
	return ret;
}

int RecuperePort	(char * retour, const Parametres * P)
{
	int ret=1;
	if (P == NULL)
		ret = -2;
	else
	{
		strcpy( retour, P->port  );
		if ( strcmp( retour, P->port) != 0)
			ret = -1;
	}
	return ret;
}

int RecupereDebug	(int    retour, const Parametres * P)
{
	int ret=1;
	if (P == NULL)
		ret = -2;
	else
	{
		retour = P->debug;
		if ( retour != P->debug )
			ret = -1;
	}
	return ret;
}

int RecupereUser 	(char * retour, const Parametres * P)
{
	int ret=1;
	if (P == NULL)
		ret = -2;
	else
	{
		strcpy( retour, P->user );
		if ( strcmp( retour, P->user) != 0)
			ret = -1;
	}
	return ret;
}

int RecuperePassword	(char * retour, const Parametres * P)
{
	int ret=1;
	if (P == NULL)
		ret = -2;
	else
	{
		strcpy( retour, P->password  );
		if ( strcmp( retour, P->password) != 0)
			ret = -1;
	}
	return ret;
}

int RecupereFic		(char * retour, const Parametres * P)
{
	int ret=1;
	if (P == NULL)
		ret = -2;
	else
	{
		strcpy( retour, P->FIC );
		if ( strcmp( retour, P->FIC) != 0)
			ret = -1;
	}
	return ret;
}

void Affichage_parametres (Parametres * P)
{
    fprintf(stdout, "Les paramètres utilisés sont :\n  serveur=%s\n  port=%s\n  debug=%d\n  user=%s\n  password=%s\n  fichier = %s\n",
    P->serveur, P->port, P->debug, P->user, P->password, P->FIC);
}


